import Config from '@/config'
import store from '@/store'

export default async function request (opt) {
   
  
    
    opt = opt || {};
    opt.url = opt.url || '';
    opt.data = opt.data || null;
    opt.method = opt.method || 'GET';
	

	let otherParam = {};
	if(!opt.responseType){
		opt.header = opt.header || {
		    "Content-Type": "application/x-www-form-urlencoded",
	       "Language": Config.curLang
		};
		otherParam.dataType = 'json'
	}else{
		opt.header = {
			"Language": Config.curLang
		}
		otherParam.responseType = opt.responseType;
		if(opt.dataType){
			otherParam.dataType = opt.dataType
		}
	}
	let token = '';
	let userInfo = uni.getStorageSync('userInfo') || '';
	if(userInfo&&userInfo.access_token){
		token = userInfo.access_token;
	}
	opt.header.Authorization = 'Bearer '+ token;
	
	  
	return new Promise((resolve,reject)=>{
		uni.request({
		    url: opt.urls ? ( Config.apiUrl+ opt.urls): (Config.baseUrl + opt.url),
		    data: opt.data,
		    method: opt.method,
		    header: opt.header,
		    ...otherParam,
		    success: res=>{
				if(res.data.state==266){
					store.commit('logout','')
				}else{
					resolve(res.data);
				}
		  },
		   fail: err=>{
			reject(err);
		  },
			complete: (res)=>{
			}
		})
  })
    
}

/** 
 * 刷新token
 * @zjf-2021-07-22
 */
function refreshToken(refresh_token) {
	return new Promise(func => {
		uni.request({
		    url: Config.apiUrl + 'v3/frontLogin/oauth/token',
		    data: {
					grant_type: 'refresh_token',
					refresh_token: refresh_token,
				},
		    method: 'POST',
		    header: {
			    "Content-Type": "application/x-www-form-urlencoded",
				"Language": Config.curLang,
				Authorization: 'Basic VVcxS2FsSnVTblppYmxFOTpVMjFHTWxsVlFrUmlNMEkxVlcxc2JtRklVa0ZWTW5oMldrYzVkUT09',
			},
		    success: res=>{
				func(res);
		  }
		})
	})
}